import java.util.HashSet;
import java.util.Set;

public class asd {
    public static void main(String[]args) {
        //目标：判断一个数独中的数字是否合法
        //设定一个初值为[["8","3",".",".","7",".",".",".","."]
        //,["6",".",".","1","9","5",".",".","."]
        //,[".","9","8",".",".",".",".","6","."]
        //,["8",".",".",".","6",".",".",".","3"]
        //,["4",".",".","8",".","3",".",".","1"]
        //,["7",".",".",".","2",".",".",".","6"]
        //,[".","6",".",".",".",".","2","8","."]
        //,[".",".",".","4","1","9",".",".","5"]
        //,[".",".",".",".","8",".",".","7","9"]]的数独，其中'.'代表空
        char[][] board = new char[][]{
                {'8','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}
        };
        Set<String> set = new HashSet<>();//特征是不重复的
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++){
                char c = board[i][j];
                if(c == '.')
                    continue;
                if(!set.add("row"+i+c) || !set.add("col"+j+c) || !set.add("box"+i/3+j/3+c)){
                    System.out.println("不合法");
                    return;
                }
            }
        }
        System.out.println("合法");
    }
}

